package com.changer.common.service.impl;

import com.changer.common.service.CacheService;
import com.changer.framework.util.GoogleJsonUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by pangk on 2016/6/15.
 */
public class StringRedisServiceImpl implements CacheService{

    private RedisTemplate redisTemplate;

    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate redisTemplate)
    {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public String get(final String key) {
        return (String)redisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection)
                    throws DataAccessException {
                byte[] keyByte = redisTemplate.getStringSerializer().serialize(key);
                if (connection.exists(keyByte)) {
                    byte[] value = connection.get(keyByte);
                    String valueString = (String)redisTemplate.getStringSerializer().deserialize(value);

                    return valueString;
                }
                return null;
            }
        });
    }

    @Override
    public void put(final String key, final Object value) {
        redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection)
                    throws DataAccessException {
                String valueString = null;
                if(value instanceof String){
                    valueString = (String)value;
                }else {
                    valueString = GoogleJsonUtils.toJson(value);
                }
                connection.set(
                        redisTemplate.getStringSerializer().serialize(key),
                        redisTemplate.getStringSerializer().serialize(valueString));
                return null;
            }
        });
    }

    @Override
    public void put(final String key, final Object value,final Date expire) {
        redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection)
                    throws DataAccessException {
                String valueString = null;
                if(value instanceof String){
                    valueString = (String)value;
                }else {
                    valueString = GoogleJsonUtils.toJson(value);
                }
                byte[] objKey = redisTemplate.getStringSerializer().serialize(key);
                connection.set(objKey,redisTemplate.getStringSerializer().serialize(valueString));
                connection.pExpireAt(objKey,expire.getTime());
                return null;
            }
        });
    }

    @Override
    public boolean remove(String key) {
        redisTemplate.delete(key);
        return true;
    }

    @Override
    public boolean removeKeys(Set keys) {
        redisTemplate.delete(keys);
        return true;
    }

    @Override
    public void incr(String key) {

    }

    @Override
    public boolean exists(String key) {
        return  redisTemplate.hasKey(key);
    }

    @Override
    public void rpush(String key, String value) {

    }

    @Override
    public List<String> getRpush(String key) {
        return null;
    }

    @Override
    public Set keys(String prefix) {
       return redisTemplate.keys(prefix);
    }
}
